﻿using AutoMapper;
using Entity.BaseInfo.DTO;
using Entity.BaseInfo.DTO.LogisticsCompany;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.LogisticsCompany;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.BaseInfo
{
    public class LogisticsCompanyService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public LogisticsCompanyService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取物流公司列表
         */
        public async Task<RequestResponseModel<PageResult<LogisticsCompanyView>>> GetLogisticsCompanyList(LogisticsCompanySearchDTO logisticsCompanySearchDTO)
        {
            // 筛选条件
            var query = _context.LogisticsCompanyView
                .WhereIF(a => a.Code.Contains(logisticsCompanySearchDTO.Code), !string.IsNullOrEmpty(logisticsCompanySearchDTO.Code))
                .WhereIF(a => a.Name.Contains(logisticsCompanySearchDTO.Name), !string.IsNullOrEmpty(logisticsCompanySearchDTO.Name))
                .WhereIF(a => a.Contact.Contains(logisticsCompanySearchDTO.Contact), !string.IsNullOrEmpty(logisticsCompanySearchDTO.Contact))
                .WhereIF(a => a.IsEnable == logisticsCompanySearchDTO.IsEnable, logisticsCompanySearchDTO.IsEnable != null)
                ;

            // 返回列表
            var logisticsCompanyList = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(logisticsCompanySearchDTO.CurrentPage, logisticsCompanySearchDTO.PageSize)
                .ToListAsync();

            // 总数据量
            var pageCount = await query.CountAsync();

            // 返回数据
            var pageResult = new PageResult<LogisticsCompanyView>(logisticsCompanyList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增物流公司
         */
        public async Task<RequestResponseModel<string>> AddLogisticsCompany(LogisticsCompanyAddDTO logisticsCompanyAddDTO)
        {
            // 新增数据
            var logisticsCompany = _mapper.Map<LogisticsCompanyModel>(logisticsCompanyAddDTO);
            logisticsCompany.CreateTime = DateTime.Now;
            logisticsCompany.Id = Guid.NewGuid().ToString();

            // 更新表
            _context.LogisticsCompany.Add(logisticsCompany);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", logisticsCompany.Id);
        }

        /**
         * 编辑物流公司
         */
        public async Task<RequestResponseModel<string>> PutLogisticsCompany(LogisticsCompanyUpdateDTO logisticsCompanyUpdateDTO)
        {
            // 校验数据
            var query = await Valid.ValidId<LogisticsCompanyModel>(_context.LogisticsCompany, logisticsCompanyUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", logisticsCompanyUpdateDTO.Id);
            }

            // 生成新数据
            var logisticsCompany = _mapper.Map(logisticsCompanyUpdateDTO, query);

            // 更新物流公司表
            _context.LogisticsCompany.Update(logisticsCompany);

            // 保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", logisticsCompany.Id);
        }

        /**
         * 删除物流公司
         */
        public async Task<RequestResponseModel<string>> DeleteLogisticsCompany(string Id)
        {
            // 校验数据
            var query = await Valid.ValidId<LogisticsCompanyModel>(_context.LogisticsCompany, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            // 删除操作
            _context.LogisticsCompany.Remove(query);

            // 保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 删除物流公司 - 批量
         */
        public async Task<RequestResponseModel<List<string>>> DeleteLogisticsCompanyMulti(List<string> Ids)
        {
            // 是否有匹配资源
            var query = await Valid.ValidIdList<LogisticsCompanyModel>(_context.LogisticsCompany, Ids);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Ids无效", Ids);
            }

            // 更新表
            _context.LogisticsCompany.RemoveRange(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Ids);
        }

        /**
         * 启用物流公司 - 批量
         */
        public async Task<RequestResponseModel<List<string>>> EnableLogisticsCompany(List<string> Ids)
        {
            // 是否有匹配资源
            var query = await Valid.ValidIdList<LogisticsCompanyModel>(_context.LogisticsCompany, Ids);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Ids无效", Ids);
            }

            // 设置启用
            query.ForEach(a =>
            {
                a.IsEnable = true;
            });

            // 更新表
            _context.LogisticsCompany.UpdateRange(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "启用成功", Ids);
        }

        /**
         * 禁用物流公司 - 批量
         */
        public async Task<RequestResponseModel<List<string>>> DisableLogisticsCompany(List<string> Ids)
        {
            // 是否有匹配资源
            var query = await Valid.ValidIdList<LogisticsCompanyModel>(_context.LogisticsCompany, Ids);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Ids无效", Ids);
            }

            // 设置禁用
            query.ForEach(a =>
            {
                a.IsEnable = false;
            });

            // 更新表
            _context.LogisticsCompany.UpdateRange(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "禁用成功", Ids);
        }
    }
}
